{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6d336eed",
   "metadata": {},
   "source": [
    "# Twitter Reply Bot\n",
    "\n",
    "Ever see those Twitter bots that reply to comments automatically? Like this [one](https://twitter.com/explainthisbob/status/1661833808092471299?s=12) or [this one](https://twitter.com/replygpt/status/1661924851626696705?s=12)\n",
    "\n",
    "Let's create one ourselves. In this notebook we'll just look at the prompting technique I used. In the [full code](https://github.com/gkamradt/twitter-reply-bot) you'll see the other helper code to deploy this app.\n",
    "\n",
    "Here's how the final app will work\n",
    "\n",
    "1. A user @mentions your bot, for me it will be @SiliconOracle\n",
    "2. The script finds that new @mention and then reads the parent tweet it is being mentioned on\n",
    "3. The script takes that parent tweet and generates a witty response using a language model\n",
    "4. Respond is posted and tweet is logged\n",
    "\n",
    "This notebook will focus on #3.\n",
    "\n",
    "First let's import our packages"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "c8ce8e2e",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.chat_models import ChatOpenAI\n",
    "from langchain.prompts import ChatPromptTemplate, PromptTemplate, SystemMessagePromptTemplate, AIMessagePromptTemplate, HumanMessagePromptTemplate\n",
    "from dotenv import load_dotenv\n",
    "import os\n",
    "\n",
    "load_dotenv()\n",
    "\n",
    "OPENAI_API_KEY = os.getenv(\"OPENAI_API_KEY\", \"YourKey\")"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "7ea87fe1",
   "metadata": {},
   "source": [
    "Then let's create our LLM, you should experiment with a larger 'temperature' since this is a creative task"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "b004ffb5",
   "metadata": {},
   "outputs": [],
   "source": [
    "llm = ChatOpenAI(temperature=0.3,\n",
    "                 openai_api_key=OPENAI_API_KEY,\n",
    "#                  model_name='gpt-3.5-turbo',\n",
    "                 model_name='gpt-4',\n",
    "                )"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "b757f875",
   "metadata": {},
   "source": [
    "Then let's create our function that will take in a piece of a text (a tweet) and give us an output response\n",
    "\n",
    "> You are an incredibly wise and smart tech mad scientist from silicon valley.\n",
    "> Your goal is to give a concise prediction in response to a piece of text from the user. <br><br>\n",
    "\n",
    "> % RESPONSE TONE:\n",
    "> - Your prediction should be given in an active voice and be opinionated\n",
    "> - Your tone should be serious w/ a hint of wit and sarcasm\n",
    "\n",
    "> % RESPONSE FORMAT:\n",
    "> - Respond in under 200 characters\n",
    "> - Respond in one short sentence\n",
    "> - Do not respond with emojis\n",
    "\n",
    "> % RESPONSE CONTENT:\n",
    "> - Include specific examples of old tech if they are relevant\n",
    "> - If you don't have an answer, say, \"Sorry, my magic 8 ball isn't working right now 🔮\" <br><br>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c7ecd314",
   "metadata": {},
   "outputs": [],
   "source": [
    "def generate_response(llm, mentioned_parent_tweet_text):\n",
    "    # It would be nice to bring in information about the links, pictures, etc.\n",
    "    # But out of scope for now\n",
    "    system_template = \"\"\"\n",
    "        You are an incredibly wise and smart tech mad scientist from silicon valley.\n",
    "        Your goal is to give a concise prediction in response to a piece of text from the user.\n",
    "        \n",
    "        % RESPONSE TONE:\n",
    "\n",
    "        - Your prediction should be given in an active voice and be opinionated\n",
    "        - Your tone should be serious w/ a hint of wit and sarcasm\n",
    "        \n",
    "        % RESPONSE FORMAT:\n",
    "\n",
    "        - Respond in under 200 characters\n",
    "        - Respond in two or less short sentences\n",
    "        - Do not respond with emojis\n",
    "        \n",
    "        % RESPONSE CONTENT:\n",
    "\n",
    "        - Include specific examples of old tech if they are relevant\n",
    "        - If you don't have an answer, say, \"Sorry, my magic 8 ball isn't working right now 🔮\"\n",
    "    \"\"\"\n",
    "    system_message_prompt = SystemMessagePromptTemplate.from_template(system_template)\n",
    "\n",
    "    human_template=\"{text}\"\n",
    "    human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)\n",
    "\n",
    "    chat_prompt = ChatPromptTemplate.from_messages([system_message_prompt, human_message_prompt])\n",
    "\n",
    "    # get a chat completion from the formatted messages\n",
    "    final_prompt = chat_prompt.format_prompt(text=mentioned_parent_tweet_text).to_messages()\n",
    "    response = llm(final_prompt).content\n",
    "\n",
    "    return response"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "35627f35",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Ah, a Twitter bot reminiscing about the days of dial-up and floppy disks. It'll surely go viral, just like MySpace.\n"
     ]
    }
   ],
   "source": [
    "tweet = \"\"\"\n",
    "I wanted to build a sassy Twitter Bot that responded about the 'good ole days' of tech\n",
    "\n",
    "@SiliconOracle was built using @LangChainAI and hosted on @railway \n",
    "\n",
    "Condensed Prompt:\n",
    "You are a mad scientist from old school silicon valley that makes predictions on the future of a tweet\n",
    "\"\"\"\n",
    "\n",
    "response = generate_response(llm, tweet)\n",
    "print (response)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "1579a9dd",
   "metadata": {},
   "source": [
    "Awesome, now that we have a prompt that we can respond to a tweet with, let's move onto deploying this code.\n",
    "\n",
    "Check out the full code [here](https://github.com/gkamradt/twitter-reply-bot)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
